package com.purebang.spaceinvaders;

import java.awt.Rectangle;

public class Enemy {
	public enum EnemyMoveDirection {
		left,
		right
	};
	
	public enum EnemyType {
		top,
		middle,
		bottom
	};
	
	private EnemyType enemyType = null;
	
	private int score;
	private int xSpeed;
	//used to help keep track of projectiles in game manager
	private int ID;
	private int projectilesFired;
	private boolean changeDirection;
	private Rectangle hitbox;
	private EnemyMoveDirection moveDirection;
	
	public final static int ENEMY_HEIGHT = 30;
	public final static int  ENEMY_WIDTH = 30;
	
	public Enemy(int x, int y, int score, int index, EnemyMoveDirection direction, EnemyType type) {
		hitbox = new Rectangle(x, y, ENEMY_WIDTH, ENEMY_HEIGHT);
		xSpeed = 2;
		projectilesFired = 0;
		changeDirection = false;
		moveDirection = direction;
		ID = index;
		enemyType = type;
		this.score = score;
	}
	
	public void update() {
		//Move enemy left
		//NOTE: Enemy hitbox width should be less than ENEMY_MAX_XLEFT
		if (changeDirection == true) {
			descend();
			return;
		}
		
		if (moveDirection != null) {
			if (moveDirection == EnemyMoveDirection.left && 
					((hitbox.x - xSpeed) > GameManager.ENEMY_MAX_XLEFT)){
				hitbox.x = hitbox.x - xSpeed;
				return;
			}else if(moveDirection == EnemyMoveDirection.left) {
				hitbox.x = hitbox.x - xSpeed;
				changeDirection = true;
				return;
			}
		}
		//Move enemy right
		//NOTE: Enemy hitbox width should be less than LEVEL_WIDTH - ENEMY_MAX_XRIGHT
		if (moveDirection != null) {
			if (moveDirection == EnemyMoveDirection.right && 
					((hitbox.x + xSpeed) < (GameManager.ENEMY_MAX_XRIGHT - ENEMY_WIDTH))){
				hitbox.x = hitbox.x + xSpeed;
				return;
			}else if(moveDirection == EnemyMoveDirection.right){
				hitbox.x = hitbox.x + xSpeed;
				//change of direction needs to occur
				changeDirection = true;
				return;
			}
		}
	}
	
	public void descend() {
		//Enemy moves down to the next level
		hitbox.y = hitbox.y + ENEMY_HEIGHT + GameManager.ENEMY_Y_PADDING;
		
		//Enemy changes directions
		if (moveDirection == EnemyMoveDirection.left)
			moveDirection = EnemyMoveDirection.right;
		else
			moveDirection = EnemyMoveDirection.left;
		
		//Enemy no longer needs to change direction
		changeDirection = false;
	}

	public int getScore() {
		return score;
	}
	
	//Used by GameManager to fire a projectile
	public void fire() {
		projectilesFired ++;
	}
	
	//Used by GameManager to keep track of when the fired projectile
	//has hit something
	public void projectileHit() {
		projectilesFired--;
	}
	
	public void resetProjectilesFired() {
		projectilesFired = 0;
	}
	public int getID() {
		return ID;
	}

	public int getProjectilesFired() {
		return projectilesFired;
	}

	public boolean isChangeDirection() {
		return changeDirection;
	}

	public Rectangle getHitbox() {
		return hitbox;
	}

	public void setxSpeed(int xSpeed) {
		this.xSpeed = xSpeed;
	}
	
	public int getxSpeed() {
		return xSpeed;
	}
	
	public EnemyType getType() {
		return enemyType;
	}
	
	public void setChangeDirection(boolean change) {
		changeDirection = change;
	}
	
	
}
